package beautiful.butterfly.drds.data_exchange.data_exchange.record.column;

import beautiful.butterfly.drds.data_exchange.data_exchange.record.column.column_cast.ColumnCast;
import beautiful.butterfly.drds.data_exchange.error_code.CommonErrorCode;
import beautiful.butterfly.drds.data_exchange.error_code.DataExchangeException;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;


public class StringColumn extends Column
{

    public StringColumn()
    {
        this((String) null);
    }

    public StringColumn(final String string)
    {
        super(string, ColumnType.STRING, (null == string ? 0 : string
                .length()));
    }


    //
    @Override
    public Boolean asBoolean()
    {
        if (null == this.getObject())
        {
            return null;
        }

        if ("true".equalsIgnoreCase(this.asString()))
        {
            return true;
        }

        if ("false".equalsIgnoreCase(this.asString()))
        {
            return false;
        }

        throw DataExchangeException.asDataExchangeException(
                CommonErrorCode.CONVERT_NOT_SUPPORT,
                String.format("String[\"%s\"]不能转为Bool .", this.asString()));
    }


    private void validateDoubleSpecific(final String string)
    {
        if ("NaN".equals(string) || "Infinity".equals(string) || "-Infinity".equals(string))
        {
            throw DataExchangeException.asDataExchangeException(
                    CommonErrorCode.CONVERT_NOT_SUPPORT,
                    String.format("String[\"%s\"]属于Double特殊类型，不能转为其他类型 .", string));
        }

        return;
    }


    @Override
    public Long asLong()
    {
        if (null == this.getObject())
        {
            return null;
        }

        this.validateDoubleSpecific((String) this.getObject());

        try
        {
            BigInteger bigInteger = this.asBigInteger();
            OverFlowChecker.validateDataIsNotOverFlowLongRange(bigInteger);
            return bigInteger.longValue();
        } catch (Exception e)
        {
            throw DataExchangeException.asDataExchangeException(
                    CommonErrorCode.CONVERT_NOT_SUPPORT,
                    String.format("String[\"%s\"]不能转为Long .", this.asString()));
        }
    }

    @Override
    public Double asDouble()
    {
        if (null == this.getObject())
        {
            return null;
        }

        String string = (String) this.getObject();
        if ("NaN".equals(string))
        {
            return Double.NaN;
        }

        if ("Infinity".equals(string))
        {
            return Double.POSITIVE_INFINITY;
        }

        if ("-Infinity".equals(string))
        {
            return Double.NEGATIVE_INFINITY;
        }

        BigDecimal bigDecimal = this.asBigDecimal();
        OverFlowChecker.validateDataIsNotOverFlowDoubleRange(bigDecimal);

        return bigDecimal.doubleValue();
    }


    @Override
    public BigDecimal asBigDecimal()
    {
        if (null == this.getObject())
        {
            return null;
        }

        this.validateDoubleSpecific((String) this.getObject());

        try
        {
            return new BigDecimal(this.asString());
        } catch (Exception e)
        {
            throw DataExchangeException.asDataExchangeException(
                    CommonErrorCode.CONVERT_NOT_SUPPORT, String.format(
                            "String [\"%s\"] 不能转为BigDecimal .", this.asString()));
        }
    }

    @Override
    public BigInteger asBigInteger()
    {
        if (null == this.getObject())
        {
            return null;
        }

        this.validateDoubleSpecific((String) this.getObject());

        try
        {
            return this.asBigDecimal().toBigInteger();
        } catch (Exception e)
        {
            throw DataExchangeException.asDataExchangeException(
                    CommonErrorCode.CONVERT_NOT_SUPPORT, String.format(
                            "String[\"%s\"]不能转为BigInteger .", this.asString()));
        }
    }

    @Override
    public String asString()
    {
        if (null == this.getObject())
        {
            return null;
        }

        return (String) this.getObject();
    }


    @Override
    public Date asDate()
    {
        try
        {
            return ColumnCast.string2Date(this);
        } catch (Exception e)
        {
            throw DataExchangeException.asDataExchangeException(
                    CommonErrorCode.CONVERT_NOT_SUPPORT,
                    String.format("String[\"%s\"]不能转为Date .", this.asString()));
        }
    }

    @Override
    public byte[] asBytes()
    {
        try
        {
            return ColumnCast.string2Bytes(this);
        } catch (Exception e)
        {
            throw DataExchangeException.asDataExchangeException(
                    CommonErrorCode.CONVERT_NOT_SUPPORT,
                    String.format("String[\"%s\"]不能转为Bytes .", this.asString()));
        }
    }
}
